package com.irun.pm.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


/**
 *公共工具类
 * @author Evy
 *
 */
public class Utils {
	//类型定义
	//人口查询
	public static final int TYPE_PEOPLE_QUERY = 1;
	//在逃人员
	public static final int TYPE_FUGITIVES_QUERY = 2;
	//违章
	public static final int TYPE_VIOLATION_QUERY = 3;
	//车辆信息
	public static final int TYPE_CARINFO_QUERY = 4;
	//驾驶员
	public static final int TYPE_DRIVER_QUERY = 5;
	//现场执法
	public static final int TYPE_SITE_QUERY = 6;
	//事故简报
	public static final int TYPE_ACCIDENT_QUERY = 7;
	
	
	//用户名，密码
	public static final String name = "李凯";
	public static final String pwd = "123";
	public static final String name1 = "test1";
	public static final String pwd1 = "123";
	public static final String name2 = "test2";
	public static final String pwd2 = "123";
	public static final String name3 = "test3";
	public static final String pwd3 = "123";
	public static final String name4 = "刘波";
	public static final String pwd4 = "123";
	public static final String PREFERENCES_NAME_KEY = "userid";
	public static final String PREFERENCES_PASSWORD_KEY = "userpassword";
	public static final String preferences_filename = "police_preferences";
	private SharedPreferences preferences;
	public Utils(Context context) {
		preferences = context.getSharedPreferences(preferences_filename, Context.MODE_PRIVATE);
	}
	
	
	
	//activity返回刷新
	public static final int ENFORCEMENT_REQUEST_CODE = 101;
	public static final int ENFORCEMENT_RESULT_CODE = 102;
	public final static int LOGIN_REQUEST_CODE = 103;
	public final static int EXIT_RESULT_CODE = 104;
	public final static int ENFORCEMENT_REQUEST_DETAILS_CODE = 104;
	public static final String ENFORCEMENT_KEY = "enforcement_details";
	public static final String ENFORCEMENT_PHOTO = "ENFORCEMENT_PHOTO";
	
	
	/**
	 * 请求服务器数据
	 * @author huangsm
	 * @date 2012-6-10
	 * @email huangsanm@gmail.com
	 * @description
	 */
	public static String getRemoteServicesData(String uri){
		StringBuffer buffer = new StringBuffer("");
		BasicHttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 4000);
		HttpConnectionParams.setSoTimeout(params, 4000);
		HttpClient client = new DefaultHttpClient(params);
		HttpGet httpGet = new HttpGet(uri);
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if(statusCode == 200){
				System.out.println("请求成功");
				HttpEntity httpEntity = response.getEntity();
				InputStream is = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
				String line;
				while ((line = reader.readLine()) != null) {
					buffer.append(line);
				}
			}else{
				System.out.println("请求失败！");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	
	/***
	 * 根据path转换bitmap
	 * path 路径
	 * w 宽度
	 * h 高度
	 */
	public static Bitmap getBitmapByPath(String path, int w, int h){
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = false;
		BitmapFactory.decodeFile(path, opts);
		int width = opts.outWidth;
		int height = opts.outHeight;
		int scale = 0;
		if(width > w || height > h){
			float scalew = ((float) width) / w;
			float scaleh = ((float) height) / h;
			scale = (int)Math.max(scalew, scaleh);
		}
		opts.inJustDecodeBounds = false;
		opts.inSampleSize = scale;
		WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
		return weak.get();
	}
}
